package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.RbacPermDao;
import com.woniuxy.entity.RbacPerm;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class RbacPermServiceImpl implements RbacPermService {
    @Autowired
    RbacPermDao rbacPermDao;
    @Override
    public Set<String> findPermBySetRoles(String username) {
        return rbacPermDao.findPermBySetRoles(username);
    }

    @Override
    public ResponseEntity findAll(int currentPage,int pageSize) {
        PageHelper.startPage(currentPage, pageSize);//只要在这里传入了分页信息
        List<RbacPerm> all = rbacPermDao.findAll();
        PageInfo pageInfo = new PageInfo(all);//就能够封装分页信息
        return new ResponseEntity(200,"请求成功！",pageInfo);
    }

    @Override
    public ResponseEntity search(int currentPage, int pageSize,String name,String code) {
        PageHelper.startPage(currentPage,pageSize);
        List<RbacPerm> list= rbacPermDao.search(name,code);
        PageInfo pageInfo = new PageInfo(list);
        return new ResponseEntity(200, "", pageInfo);
    }

    @Override
    public ResponseEntity addOne(RbacPerm rbacPerm) {
        int i = rbacPermDao.addOne(rbacPerm);
        if (i>0) {
            return new ResponseEntity(200, "新增成功", null);
        }else {
            return new ResponseEntity(500, "新增失败", null);
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = rbacPermDao.deleteOne(id);
        if (i>0) {
            return new ResponseEntity(200, "删除成功", null);
        }else {
            return new ResponseEntity(500, "删除失败", null);
        }
    }

    @Override
    public ResponseEntity updateOne(RbacPerm rbacPerm) {
        int i = rbacPermDao.editOne(rbacPerm);
        if (i>0){
            return new ResponseEntity(200,"成功修改");
        }else {
            return new ResponseEntity(500,"修改失败");
        }
    }

    @Override
    public ResponseEntity editType(int id) {
        int i = rbacPermDao.editType(id);
        if (i>0){
            return new ResponseEntity(200,"成功更新"+id+"这条数据状态",null);
        }else {
            return new ResponseEntity(500,"更新失败",null);
        }
    }
}
